<?php

namespace App\HttpController;

use App\Service\VoteService;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\Mysqli\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="投票活动")
 */
class Vote extends BaseController
{
    /**
     * @Api(name="投票开关", path="/Vote/getVoteStatus", version="1.0.0")
     * @ApiDescription(value="投票开关")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    public function getVoteStatus(): bool
    {
        $res = VoteService::getInstance()->getVoteStatus();
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="投票人员名单", path="/Vote/getVoteItem", version="1.0.0")
     * @ApiDescription(value="投票人员名单")
     * @Method(allow={POST})
     * @Param(name="id", description="id", from={JSON})
     * @return bool
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getVoteItem($id): bool
    {
        $res = VoteService::getInstance()->getVoteItem($id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }

        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="投票", path="/Vote/vote", version="1.0.0")
     * @ApiDescription(value="投票")
     * @Method(allow={POST})
     * @Param(name="vid", description="人员id", required="人员必选", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function vote($vid): bool
    {
        $res = VoteService::getInstance()->vote($this->uid, $vid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }

        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="投票结果排名", path="/Vote/rank", version="1.0.0")
     * @ApiDescription(value="投票结果排名")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function rank(): bool
    {
        $res = VoteService::getInstance()->rank();
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }

        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}